package com.unswift;

import java.io.File;

import com.unswift.utils.FileUtils;
import com.unswift.utils.ObjectUtils;
import com.unswift.utils.StringUtils;

public class ApiOptimize {

	public static void main(String[] args) {
		String path="D:\\develop\\workspace-cloud\\unswift-quick";
		File file=new File(path);
		handleFile(file);
	}
	
	public static void handleFile(File file) {
		if(file.isFile()) {
			if(file.getName().endsWith(".java")) {
				String content=FileUtils.reader(file, "UTF-8");
				StringBuilder newContent=new StringBuilder();
				boolean exists=false;
				int first=0;
				int index=content.indexOf(", params=\"");
				while(index!=-1) {
					newContent.append(content, first, index).append(", params=@ApiField(\"");
					index+=", params=\"".length();
					first=index;
					index=content.indexOf("\"", first)+1;
					newContent.append(content, first, index).append(")");
					first=index;
					index=content.indexOf(", params=\"", first);
					exists=true;
				}
				newContent.append(content.substring(first));
				content=newContent.toString();
				
				newContent=new StringBuilder();
				first=0;
				index=content.indexOf(", params = \"");
				while(index!=-1) {
					newContent.append(content, first, index).append(", params = @ApiField(\"");
					index+=", params = \"".length();
					first=index;
					index=content.indexOf("\"", first)+1;
					newContent.append(content, first, index).append(")");
					first=index;
					index=content.indexOf(", params = \"", first);
					exists=true;
				}
				newContent.append(content.substring(first));
				content=newContent.toString();
				
				newContent=new StringBuilder();
				first=0;
				index=content.indexOf(", params={");
				String subContent;
				String[] subContentArray;
				while(index!=-1) {
					newContent.append(content, first, index).append(", params={@ApiField(");
					index+=", params={".length();
					first=index;
					index=content.indexOf("}", first);
					subContent=content.substring(first, index);
					if(subContent.indexOf(", ")==-1) {
						subContentArray=subContent.split(",");
						subContent=StringUtils.join(subContentArray, "), @ApiField(");
					}else {
						subContentArray=subContent.split(", ");
						subContent=StringUtils.join(subContentArray, "), @ApiField(");
					}
					newContent.append(subContent).append(")}");
					first=index+1;
					index=content.indexOf(", params={", first);
					exists=true;
				}
				newContent.append(content.substring(first));
				content=newContent.toString();
				
				newContent=new StringBuilder();
				first=0;
				index=content.indexOf(", params = {");
				while(index!=-1) {
					newContent.append(content, first, index).append(", params = {@ApiField(");
					index+=", params = {".length();
					first=index;
					index=content.indexOf("}", first);
					subContent=content.substring(first, index);
					if(subContent.indexOf(", ")==-1) {
						subContentArray=subContent.split(",");
						subContent=StringUtils.join(subContentArray, "), @ApiField(");
					}else {
						subContentArray=subContent.split(", ");
						subContent=StringUtils.join(subContentArray, "), @ApiField(");
					}
					newContent.append(subContent).append(")}");
					first=index+1;
					index=content.indexOf(", params = {", first);
					exists=true;
				}
				newContent.append(content.substring(first));
				content=newContent.toString();
				
				newContent=new StringBuilder();
				first=0;
				index=content.indexOf(", returns = \"");
				while(index!=-1) {
					newContent.append(content, first, index).append(", returns = @ApiField(\"");
					index+=", returns = \"".length();
					first=index;
					index=content.indexOf("\"", first)+1;
					newContent.append(content, first, index).append(")");
					first=index;
					index=content.indexOf(", returns = \"", first);
					exists=true;
				}
				newContent.append(content.substring(first));
				content=newContent.toString();
				
				newContent=new StringBuilder();
				first=0;
				index=content.indexOf(", returns=\"");
				while(index!=-1) {
					newContent.append(content, first, index).append(", returns=@ApiField(\"");
					index+=", returns=\"".length();
					first=index;
					index=content.indexOf("\"", first)+1;
					newContent.append(content, first, index).append(")");
					first=index;
					index=content.indexOf(", returns=\"", first);
					exists=true;
				}
				newContent.append(content.substring(first));
				content=newContent.toString();
				if(exists && content.indexOf("import com.unswift.annotation.api.ApiField;")==-1) {
					content=content.replace("import com.unswift.annotation.api.Api;", "import com.unswift.annotation.api.Api;"+FileUtils.nextLine()+"import com.unswift.annotation.api.ApiField;");
					content=content.replace("import com.unswift.annotation.api.ApiEntity;", "import com.unswift.annotation.api.ApiEntity;"+FileUtils.nextLine()+"import com.unswift.annotation.api.ApiField;");
				}
				FileUtils.writer(file, content, "UTF-8", false);
			}
		}else {
			File[] list=file.listFiles();
			if(ObjectUtils.isNotEmpty(list)) {
				for (File child : list) {
					handleFile(child);
				}
			}
		}
	}
}
